package com.aiinterview.subjectservice.service.impl;

import com.aiinterview.common.constant.MessageConstant;
import com.aiinterview.common.exception.AccountNotFoundException;
import com.aiinterview.common.exception.PasswordErrorException;
import com.aiinterview.model.subject.entity.LoginUser;
import com.aiinterview.model.subject.entity.User;
import com.aiinterview.subjectservice.mapper.UserMapper;
import com.aiinterview.subjectservice.service.user.UserLoginService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.LoginException;

@Service
@Slf4j
public class UserLoginServiceImpl implements UserLoginService {

    @Autowired
    private UserMapper userMapper;


    /**
     * 登录模块
     * @param loginUser
     * @return
     */
    @Override
    public User login(LoginUser loginUser) {
        log.info("登录时传入的参数:{}",loginUser);
        String password = loginUser.getPassword();
        if(StringUtils.isEmpty(loginUser.getUserName()) || StringUtils.isEmpty(loginUser.getPassword()))
            return null;
        User user = userMapper.getUserByUserName(loginUser.getUserName());
        log.info("登录时查询到的用户:{}",user);
        if(user == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if(!user.getPassword().equals(password)){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        return user;
    }
}
